package com.speedchina.common.download.base;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.speedchina.common.exception.ConverterException;
import com.speedchina.framework.utils.ApplicationContextHolder;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 字典转换通用类
 * @Author xz
 * Data: 2021/5/14
 */
public abstract class BaseConverter implements Converter<String> {

    /**
     * 创建本地缓存，存放字典，用字典 type 当key TODO 此处的时效性太差，不能用
     */
    private static final Map<String, Map<String, String>> dictCache = new ConcurrentHashMap<String, Map<String, String>>();
    /**
     * 获取字典类型
     * @date:  2021/5/14
     * @param
     * @return java.lang.String
     * @throws
     */
    public abstract String getDictType();

    public String convertToJavaDataMsg(){
        return "找不到匹配";
    }

    @Override
    public Class supportJavaTypeKey() {
        return null;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return null;
    }

/*    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String dictType = getDictType();
        System.out.println(cellData.getData());
        //反向替换
        if (!StringUtils.isEmpty(cellData.getData()) ){
            //告诉我你的字典类型，先从缓存中获取
            Map<String, String> dictItemMap = dictCache.get(dictType);
            if (dictItemMap == null) {
                //没拿到调用实现方法获取字典
                GetDictMapping queryDictItem = ApplicationContextHolder.getBean(GetDictMapping.class);
                if (queryDictItem != null){
                    dictItemMap = queryDictItem.getDictMappingReverse(dictType);
                    //放入缓存
                    dictCache.put(dictType,dictItemMap);
                }
            }
            String v = dictItemMap.get(cellData.getData());
            return (v == null ? Convert.toStr(cellData.getData()) : v);
        }
        return Convert.toStr(cellData.getData());
    }*/

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {

        if (!StringUtils.isEmpty(cellData.getData())){
            //告诉我你的字典类型，先从缓存中获取
            Map<String, String> dictItemMap = getDictItemMap();
            if (null != dictItemMap){
                String value = getKey(dictItemMap,cellData.getData().toString());
                //只有获取到值才能返回字符
                if (!StringUtils.isEmpty(value)) {
                    return value;
                }
                throw new ConverterException(convertToJavaDataMsg());
            }
        }
        //获取不到对应值需要抛出异常，避免脏数据
        //throw new ExcelDataConvertException(null,excelContentProperty.getHead().getColumnIndex(),cellData,excelContentProperty,"无法匹配到字典!");
       return cellData.toString();
}

    @Override
    public CellData convertToExcelData(String value, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    Map<String, String> dictItemMap = getDictItemMap();
    if (null != dictItemMap) {
        String v = dictItemMap.get(value);
        //返回转换的值，匹配不到直接写出value
        return new CellData(v == null ? value : v);
    }
    return new CellData(value);
    }

    /**
     * 获取字典
     * author: ZhangWC
     * date: 2021/6/28
     * []
     * @return java.util.Map<java.lang.String,java.lang.String>
     */
    private Map<String, String> getDictItemMap(){
        String dictType = getDictType();
        Map<String, String> dictItemMap = dictCache.get(dictType);
        if (dictItemMap == null) {
            //没拿到调用实现方法获取字典
            GetDictMapping queryDictItem = ApplicationContextHolder.getBean(GetDictMapping.class);
            if (queryDictItem != null){
                dictItemMap = queryDictItem.getDictMapping(dictType);
                //放入缓存
                dictCache.put(dictType,dictItemMap);
            }
        }
        return dictItemMap;
    }

    /**
     * 根据key获取value值
     * author: ZhangWC
     * date: 2021/6/28
     * [map, value]
     * @return java.lang.String
     */
    public String getKey(Map<String,String> map, String value){
        //Map,HashMap并没有实现Iteratable接口.不能用于增强for循环.
        for(String getKey: map.keySet()){
            if(map.get(getKey).equals(value)){
                return getKey;
            }
        }
        return "";
        //这个key肯定是最后一个满足该条件的key.
    }

}
